<!-- user表单弹窗 -->
<form id="banner-form" lay-filter="banner-form"
	class="layui-form model-form">
	<input name="id" type="text" class="layui-hide"/>
	<div class="layui-form-item">
		<div class="layui-form-item">
			<label class="layui-form-label">跳转地址</label>
			<div class="layui-input-block">
				<input name="url" placeholder="请输入跳转地址" type="text"
					class="layui-input" maxlength="50" lay-verify="required" required disabled/>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">描述</label>
			<div class="layui-input-block">
				<input name="describe" placeholder="请输入描述" type="text"
					class="layui-input" />
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">状态</label>
			<div class="layui-input-block">
				<input type="radio" name="state" value="0" title="显示" checked /> <input
					type="radio" name="state" value="1" title="隐藏" />
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">上传图片</label>
			<div class="layui-input-block">
				<div class="layui-upload-list">
					<input id="picPath" name="picPath" class="layui-hide" /> 
					<img class="layui-upload-img" id="demo1" style="width: 150px; height: 100px; border-color: #D2D2D2 !important;">
					<button type="button" class="layui-btn" id="test1"
						style="margin-right: 25px;">选择</button>
				</div>
			</div>
		</div>
	</div>

	<div class="layui-form-item model-form-footer">
		<button class="layui-btn layui-btn-primary" type="button"
			ew-event="closeDialog">取消</button>
		<button class="layui-btn" lay-filter="form-submit" lay-submit>保存</button>
	</div>
</form>
<script>
	layui.use([ 'layer', 'admin', 'form', 'formSelects', 'upload' ],
			function() {
				var $ = layui.jquery;
				var layer = layui.layer;
				var admin = layui.admin;
				var form = layui.form;
				var upload = layui.upload;
				var formSelects = layui.formSelects;

				form.render('radio');
				form.render('select');

				var url = 'system/banner/add';
				// 回显banner数据
				var banner = admin.getTempData('t_banner');
				if (banner) {
					url = 'system/banner/update';
					$("#demo1").attr("src",banner.picPath);
					
					$("input[name=state][value=0]").attr("checked",
							banner.state == 0 ? true : false);
					$("input[name=state][value=1]").attr("checked",
							banner.state == 1 ? true : false);
					form.val('banner-form', banner);
				}

				//表单提交事件
				form.on('submit(form-submit)', function(data) {
					layer.load(2);
					admin.req(url, data.field, function(data) {
						layer.closeAll('loading');
						if (data.code == 1) {
							layer.msg(data.msg, {
								icon : 1
							});
							location.reload();
						} else {
							layer.msg(data.msg, {
								icon : 2
							});
						}
					}, "post");
					return false;
				});

				//普通图片上传
            var uploadInst = upload.render({
                elem: '#test1',
                url: 'system/banner/upload/',
                type: 'images',
                accept: 'images',
                size: 1024*10 ,//限定大小
                choose: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file,
                                          result) {
                        $('#demo1').attr("src", result);//图片链接（base64）

                    });
                }
                , done: function (data) {
                    if (data.code == 1) {
                        $("#picPath").val(data.data);
                    } else {
                        layer.msg(data["msg"], {
                            icon: 2
                        });
                        $('#demo1').attr("src", demoUrl);
                    }
                }
                , error: function () {
                    layer.msg('图片上传失败', {
                         icon: 2
                     });
                    $('#demo1').attr("src", "");
                }

            });
		});
</script>